package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.DaiYanTuanDaoImpl;
import com.ysd.entity.DaiYanTuan;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminDaiYanTuanServlet extends BaseServlet {
	
	/**
	 * 查询所有代言团队 人员
	 * localhost:8080/qiuxieshangcheng/adminDaiYanTuan?reqName=delDaiYanTuan
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDaiYanTuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DaiYanTuanDaoImpl dyDao = new DaiYanTuanDaoImpl();//创建实现类对象
		List<DaiYanTuan>  dyList =     dyDao.getDaiYanTuanList();//查到的代言团集合
		//发送Ajax请求，服务器基本会返回json格式字符串
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dyList);
		
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
	
	public void delDaiYanTuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int dyId = Integer.parseInt(request.getParameter("dytId"));
		DaiYanTuanDaoImpl dyDao = new DaiYanTuanDaoImpl();
		int dy= dyDao.deleteDaiYanTuanId(dyId);//调用删除的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (dy>0) {//删除成功
			out.print(Result.toClient(true,"删除成功" ,""));//{"success":true,"message":"删除成功"，"remark":""}
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败"，"remark":""}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 添加
	 * localhost:8080/qiuxieshangcheng/adminDaiYanTuan?reqName=addDaiYanTuan
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addDaiYanTuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		DaiYanTuanDaoImpl dyDao = new DaiYanTuanDaoImpl();//实现类对象
		String daiYanLoGo = request.getParameter("daiYanLoGo");
		String daiYanRenName = request.getParameter("daiYanRenName");
		String daiYanZhiWei = request.getParameter("daiYanZhiWei");
		if (daiYanLoGo !=null&& !daiYanLoGo.equals("")) {
			List<DaiYanTuan> dyList =  dyDao.getDYTByName(daiYanRenName);
			if (dyList.size()==0) {
				DaiYanTuan dy = new DaiYanTuan(0, daiYanLoGo, daiYanRenName, daiYanZhiWei);
				
				
				
				  int n=dyDao.addDYT(dy);
				  if(n>0) {
					  out.print(Result.toClient(true, "添加成功",""));
				  }else {
					  out.print(Result.toClient(false, "添加失败",""));
				  }
			} else {
				 out.print(Result.toClient(false, "已存在",""));
			}
			
			
		}
		
		
		  
		  out.flush();
		  out.close();
		  
	}
	
	/**
	 * localhost:8080/qiuxieshangcheng/adminDaiYanTuan?reqName=getDaiYanTuanById
	 * 
	 */
	
	public void getDaiYanTuanById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		DaiYanTuanDaoImpl dyDao = new DaiYanTuanDaoImpl();//实现类对象
		int id = Integer.parseInt(request.getParameter("dId"));
		DaiYanTuan dy=dyDao.getDYTById(id);
		
		Gson gson = new Gson();
		String res = gson.toJson(dy);
		
		out.print(res);
		  
		  out.flush();
		  out.close();
		  
	}
	
	/**
	 * localhost:8080/qiuxieshangcheng/adminDaiYanTuan?reqName=updateDaiYanTuanById
	 * 
	 */
	
	public void updateDaiYanTuanById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		DaiYanTuanDaoImpl dyDao = new DaiYanTuanDaoImpl();//实现类对象
		
		int daiYanTuanId = Integer.parseInt(request.getParameter("dyId"));
		String  daiYanLoGo= request.getParameter("dyLoGo");
		String  daiYanRenName= request.getParameter("dyName");
		String  daiYanZhiWei= request.getParameter("dyZhiWei");
		
		List<DaiYanTuan> dyList= dyDao.getDYTByName(daiYanRenName);
		
		if (dyList.size()==0) {//没有内容
			DaiYanTuan dy = new DaiYanTuan(daiYanTuanId, daiYanLoGo, daiYanRenName, daiYanZhiWei);
			int n=dyDao.updateDYT(dy);
			if (n>0) {
				out.print(Result.toClient(true, "修改成功",""));
			} else {
				out.print(Result.toClient(false, "修改失败",""));
			}
		} else {//不能用
			out.print(Result.toClient(false, "代言人已存在",""));
		}
		
		
		
		
		  
		  out.flush();
		  out.close();
		  
	}
	

}
